package com.hanyxx.io;

import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;

/**
 * @author layman
 * @date 2021/3/9
 */
public class Demo06 {
    private static Properties p = new Properties();
    static{
        // 设置key和value
        p.setProperty("name1","outerMan");
        p.setProperty("name2","layman");
    }
    
    public static void main(String[] args) {
        //test01();
        test02();
    }
    // 构造带有默认属性集的Properties
    private static void test02() {
        Properties p1 = new Properties(p);
        p1.setProperty("name1","Lily");
        p1.setProperty("name3","刘大脑袋");
        p1.setProperty("name4","Rose");
        // p1中的name1和p中重复了。
        System.out.println(p1.getProperty("name1"));
        // 获取属性集中key的set集合
        Set<String> set = p1.stringPropertyNames();
        System.out.println(set);
        // 获取属性集中key的枚举集
        Enumeration<?> enums = p1.propertyNames();
        while (enums.hasMoreElements()) {
            System.out.print(enums.nextElement() + " ");
        }
    }
    // 构造空Properties
    private static void test01() {
        // 根据存在的key获取value
        System.out.println(p.getProperty("name1"));
        // 根据不存在的key获取value
        System.out.println(p.getProperty("long"));
        // 根据key获取value，如果没有，返回默认值
        System.out.println(p.getProperty("long", "18cm"));
        System.out.println(p);
    }
}
